//
//  ASJActor.m
//  ASJ001
//
//  Created by zephyrhan on 11/13/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "ASJActor.h"
#import "ASJSkillNoLoopSkill.h"
#import "ASJUniqueSkill.h"
#import "ASJSkillActorAttack.h"
#import "ASJGestureRecoginzer.h"
#import "ASJGlobalDefine.h"
#import "ASJUSkillRain.h"


@implementation ASJActor

-(id)init
{
    if ((self = [super init]))
    {
        self.movingSpeed = CGPointMake(0.0, 0.0);
        self.ATP = 50;
        self.HP = 20;
        self.attackRate = 2;
        self.attackRange = 70.0;

        self.currentHP = HP;
        self.currentAttackRate = attackRate;
        self.currentMovingSpeed = movingSpeed;

        [[NSNotificationCenter defaultCenter]  addObserver:self selector:@selector(useUniqueSkill:) name:GESTURE_RECOGNIZED object:nil];
    }
    return self;
}


- (void)dealloc {
    [uniqueSkillNames release];
    
    [super dealloc];
}


-(void)loadSkills
{
    assert(worldLayer != nil);
    
    ASJSkill* walk = [[ASJSkill alloc] initWithSkillName:@"actor_walk" frameCount:6 inStage:worldStage];
    [self setSkill:walk forName:@"actor_walk"];
    
    ASJSkill* attack = [[ASJSkillActorAttack alloc] initWithSkillName:@"actor_attack" frameCount:6 inStage:worldStage];
    [self setSkill:attack forName:@"actor_attack"];
    [attack release];
    
    ASJSkill* die = [[ASJSkillNoLoopSkill alloc] initWithSkillName:@"actor_die" frameCount:6 comboTarget:worldLayer action:@selector(gameOver) inStage:worldStage];
    die.skillPriority = 10;
    [self setSkill:die forName:@"actor_die"];
    [die release];
    
    ASJSkill* suffer = [[ASJSkillNoLoopSkill alloc] initWithSkillName:@"actor_suffer" frameCount:6 inStage:worldStage];
    [self setSkill:suffer forName:@"actor_suffer"];
    [suffer release];
    
    ASJSkill* stand = [[ASJSkill alloc] initWithSkillName:@"actor_stand" frameCount:6 inStage:worldStage];
    [self setSkill:stand forName:@"actor_stand"];
    [stand release];
    
//    typedef enum {
//        
//        ASJGESTYPE_V = 1, 
//        ASJGESTYPE_H = 2,
//        ASJGESTYPE_R = 4,
//        ASJGESTYPE_M = 8, 
//        ASJGESTYPE_E = 16,
//        ASJGESTYPE_A = 32,
//        ASJGESTYPE_T = 64
//        
//    } ASJGestureType;
    uniqueSkillNames = [[NSArray alloc] initWithObjects:@"rain", @"ASJGESTYPE_H", @"shield", @"ASJGESTYPE_M"
                        , @"ASJGESTYPE_E", @"ASJGESTYPE_A", @"ASJGESTYPE_T", nil];
    
    // unique skills
    ASJSkill* rain = [[ASJUSkillRain alloc] initWithUniqueSkillName:@"rain" inStage:worldStage];
    rain.skillPowser = 5.0;
    rain.coolTime = 8.0;
    [self setSkill:rain forName:@"rain"];
    [rain release];
}


- (NSString*)uniqueSkillNameByType:(ASJGestureType)skillType {
    int index = -1;
    
    while (skillType > 0) {
        skillType = skillType >> 1;
        ++index;
    }
    
    if (index < [uniqueSkillNames count]) {
        return [uniqueSkillNames objectAtIndex:index];
    } else {
        assert(NO);

        return nil;
    }
}


- (void)useUniqueSkill:(NSNotification*)not {
    NSNumber* skillType = not.object;
    NSString* skillName = [self uniqueSkillNameByType:[skillType intValue]];
    
    ASJSkill* skill = [self skillByName:skillName];
    if (skill == nil || (skill.currentCoolTime > 0)) {
        return ;
    }

    CCSprite* fuzhen = [CCSprite spriteWithFile:[NSString stringWithFormat:@"%@_f.png", skillName]];
    fuzhen.position = worldPos(0.5, 0.5);
    [worldLayer addChild:fuzhen];
    
    CCFadeOut* fadeOut = [CCFadeOut actionWithDuration:2.0];
    CCCallFuncO* step2 = [CCCallFuncO actionWithTarget:self selector:@selector(useUniqueSkillStep2:) object:skillName];
    CCSequence* seq = [CCSequence actions:fadeOut, step2, nil];
    [fuzhen runAction:seq];
}


- (void)useUniqueSkillStep2:(NSString*)skillName {
    [self runSkillByName:skillName];
}

@end
